package com.myProject.serves.Impl;

import com.myProject.bean.TMouse;
import com.myProject.dao.MouseDao;
import com.myProject.serves.MouseSev;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionUtil;

import java.util.List;

public class MouseSevImpl implements MouseSev {
    @Override
    public List<TMouse> getMouseList(TMouse mouse) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        MouseDao md = sqlSession.getMapper(MouseDao.class);
        List<TMouse> mouseList = md.getMouseList(mouse);
        sqlSession.clearCache();
        return mouseList;
    }

    @Override
    public List<TMouse> findMouseList(TMouse mouse) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        MouseDao md = sqlSession.getMapper(MouseDao.class);
        List<TMouse> mouseList = md.getMouseList(mouse);
        sqlSession.clearCache();
        return mouseList;
    }

    @Override
    public PageInfo<TMouse> showMouseByPage(int pageNo, int pageSize, TMouse mouse) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        MouseDao md = sqlSession.getMapper(MouseDao.class);
        List<TMouse> list;
        PageHelper.startPage(pageNo, pageSize);
        if (mouse.getName() != null) {
            list = md.findMouseList(mouse);
        } else {
            list = md.getMouseList(mouse);
        }
        PageInfo<TMouse> pages = new PageInfo<TMouse>(list);
        SqlSessionUtil.commit();
        return pages;
    }

    @Override
    public boolean addMouse(TMouse mouse) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        MouseDao md = sqlSession.getMapper(MouseDao.class);
        int x = md.addMouse(mouse);
        if(x!=0){
            SqlSessionUtil.commit();
            sqlSession.close();
            return true;
        }
        return false;
    }
}
